package com.lelefly.base.controller;

import java.lang.reflect.Type;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lelefly.base.model.FlyDetailBean;
import com.lelefly.base.model.FlyHistoryPage;
import com.lelefly.base.model.LeleFlyHistoryDetailBean;
import com.lelefly.base.model.LeleFlyRequestBean;
import com.lelefly.base.model.Result;
import com.lelefly.base.po.LeleflyHistoryInfo;
import com.lelefly.base.serviceImp.LeleFlyInfoService;
import com.lelefly.constant.Constant;

@Controller
@RequestMapping("/fly_info")
public class LeleFlyInfoController {

	private static final Logger logger = LoggerFactory.getLogger(LeleFlyInfoController.class);
	
	@Resource
	private LeleFlyInfoService service;
	
	@RequestMapping(value="/query_real_time_fly",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result getFlyingInfo(@RequestBody String bean){
		Gson gson = new Gson();
		Type type = new TypeToken<LeleFlyRequestBean>(){}.getType();
		LeleFlyRequestBean requestBean = gson.fromJson(bean,type);
		Result result = service.queryRealTimeFly(requestBean.getPlaneid());
		return result;
	}
	
	@RequestMapping(value="/query_fly_history")
	@ResponseBody
	public Result<LeleflyHistoryInfo> queryFlyHistory(FlyHistoryPage page){
		Result<LeleflyHistoryInfo> result = new Result<>();
		if (page == null
				|| StringUtils.isEmpty(page.getPlaneId())) {
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("飞机信息不能为空");
			return result;
		}
		result = service.queryFlyHistory(page);
		return result;
	}
	
	@RequestMapping(value="/query_fly_detail")
	@ResponseBody
	public Result<LeleFlyHistoryDetailBean> queryFlyDetailInfo(FlyDetailBean bean){
		Result<LeleFlyHistoryDetailBean> result = new Result<>();
		if (bean == null
				|| StringUtils.isEmpty(bean.getHistoryId())
				|| StringUtils.isEmpty(bean.getPlaneId())) {
			result.setMes("信息不正确");
			result.setState(Constant.HTTP_STATUS_FAIL);
			return result;
		}
		result = service.queryFlyDetailInfo(bean);
		return result;
	}
}
